package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeMobileActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ed_mobile)
    EditText edMobile;
    @BindView(R.id.iv_mobile_clear)
    ImageView ivMobileClear;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.tv_change)
    TextView tvChange;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.text)
    TextView text;

    private int countDown;
    private int type;//1修改手机号，2设置微信号

    public static void actionStar(Context context,int type) {
        Intent intent = new Intent(context, ChangeMobileActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_change_mobile;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type",-1);
        if(type == -1){
            ToastUtil.showDataErrorToast(context);
            finish();
            return;
        }
        if(type == 2){
            llCode.setVisibility(View.GONE);
            text.setText("设置微信号");
            edMobile.setInputType(InputType.TYPE_CLASS_TEXT);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_back, R.id.iv_mobile_clear, R.id.tv_get_code, R.id.tv_change})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_mobile_clear:
                edMobile.setText("");
                break;
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.tv_change:
                if(type == 1){
                    checkData();
                }else{
                    setWxAccount();
                }
                break;
        }
    }

    private void setWxAccount() {
        String account = edMobile.getText().toString().trim();
        if(StringUtils.isEmpty(account)){
            ToastUtil.showToast(context,"微信号未填写");
            return;
        }
        Map map = new HashMap();
        map.put("wx_num",account);

        OkGoUtil.postRequest(getContext(), UrlConstants.SET_WX_ACCOUNT, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        SPUtils.setStringData(context, Constants.WX_ACCOUNT,account);
                        finish();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void checkData() {
        String mobile = edMobile.getText().toString().trim();
        if(StringUtils.isEmpty(mobile)){
            ToastUtil.showToast(context,"手机号未填写");
            return;
        }

        String code = edCode.getText().toString().trim();
        if(StringUtils.isEmpty(mobile)){
            ToastUtil.showToast(context,"验证码未填写");
            return;
        }

        Map map = new HashMap();
        map.put("mobile",mobile);
        map.put("captcha",code);

        OkGoUtil.postRequest(getContext(), UrlConstants.CHANGE_MOBILE, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        SPUtils.setStringData(context, Constants.MOBILE,mobile);
                        finish();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void getCode() {

        String mobile = edMobile.getText().toString().trim();
        if(StringUtils.isEmpty(mobile)){
            ToastUtil.showToast(context,"手机号未填写");
            return;
        }

        Map map = new HashMap();
        map.put("mobile",mobile);
        map.put("event","changemobile");

        OkGoUtil.postRequest(getContext(), UrlConstants.GET_CODE, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        startCountDown();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void startCountDown() {
        countDown = 60;
        tvGetCode.setText("重新获取("+countDown+")");
        tvGetCode.setClickable(false);
        mHandler.sendEmptyMessageDelayed(0,1000);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if(countDown > 0){
                        countDown --;
                        tvGetCode.setText("重新获取("+countDown+")");
                        mHandler.sendEmptyMessageDelayed(0,1000);
                    }else{
                        tvGetCode.setText("重新获取");
                        tvGetCode.setClickable(true);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mHandler != null){
            mHandler.removeMessages(0);
            mHandler = null;
        }
    }
}